package com.siwei.demo.controller;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;

public class AxisUtils {

    public static String callService(String serviceUrl, String methodName, Object[] args) {
        // 创建一个服务(service)调用(call)
        String ret = "";
        try {
            Service service = new Service();
            // 通过service创建call对象
            Call call = (Call) service.createCall();
            // 设置service所在URL
            call.setTargetEndpointAddress(
                    new java.net.URL(serviceUrl));

            call.setOperationName(methodName);//设置访问的方法名
            ret = (String) call.invoke(args);// Object 数组封装了传递的参数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static String startWebService(String cs) {
        String res = "";
        try {
            res = callService("http://127.0.0.1:8080/axis/servers/hello?wsdl", "dockInfoInsert", new Object[]{cs});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }



    public static void test() throws Exception{
        Service service = new Service();
        Call call = null;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));
            call.setOperationName(new QName("http://WebXml.com.cn/","getWeatherbyCityName"));
            call.addParameter(new QName("http://WebXml.com.cn/", "theCityName"),XMLType.SOAP_VECTOR,ParameterMode.IN);
            call.setReturnType(XMLType.SOAP_VECTOR);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");
            Object obj=call.invoke(new Object[]{"广州"});
            System.out.println(obj.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        test();
    }
}